package level.editor.view.action.file;

import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;

import level.editor.control.main.Main;
import level.editor.control.main.MainTabbed;
import level.editor.view.action.ActionConstants;

public class CloseAction extends FileAction
{
	public CloseAction(MainTabbed tabbed)
	{
		super("Close", tabbed, ActionConstants.ICON_DIR + "exit.png");
	}

	public void action()
	{
		if (tabbed.getSelectedMain() != null)
		{
			Main main = tabbed.getSelectedMain();
			if (main.isDirty())
			{
				int response = JOptionPane.showConfirmDialog(null, "The file "
						+ main.getName()
						+ " has been modified. Do you want to save it?",
						"Closing modified file",
						JOptionPane.YES_NO_CANCEL_OPTION,
						JOptionPane.WARNING_MESSAGE);
				if (response == JOptionPane.YES_OPTION)
				{
					if (main.saveAs())
					{
						main.setDirty(Main.NOT_DIRTY);
						main.updateFile();
					}
					else
						return;
				}
				else if (response == JOptionPane.CANCEL_OPTION)
					return;
			}
			tabbed.remove(main.getScroll());
		}
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		action();
	}

}
